﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CustomerRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-07-31
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using YouWei.Infrastructure.Models;
using Dapper;
using YouWei.Utility;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///客户 仓储层
    /// </summary>	
    public class CustomerRepository : BaseRepository<Customer>
    {

        #region 构造

        public CustomerRepository() : base() { }

        public CustomerRepository(string connectionString) : base(connectionString) { }

        public CustomerRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 查询用户列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<Customer> Pages(int pageIndex, int pageSize, CustomerSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT  T1.* FROM [Customer] T1 WITH(NOLOCK)  LEFT JOIN UserCustomerRel T2 ON T1.CustomerID=T2.CustomerID" +
                " WHERE T1.IsActive=1 AND T2.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }

            sb.Append(" ORDER BY T1.CustomerID DESC");
            return Pages(pageIndex, pageSize, sb.ToString(), dparams);
        }

        /// <summary>
        /// 根据手机号获取用户信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public Customer GetCustomerByMobile(string mobile)
        {
            StringBuilder sb = new StringBuilder("SELECT * FROM Customer T WITH(NOLOCK) ");
            sb.Append(" WHERE T.Mobile = @Mobile");
            return FirstOrDefault(sb.ToString(), new { Mobile = mobile });
        }

        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(CustomerSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (model.UserID > 0)
            {
                sb.Append(" AND T2.UserID=@UserID");
                dparams.Add("UserID", model.UserID, System.Data.DbType.Int32);
            }
            if (!string.IsNullOrEmpty(model.Mobile))
            {
                sb.Append(" AND T1.Mobile=@Mobile");
                dparams.Add("Mobile", model.Mobile, System.Data.DbType.String);
            }
            if (!string.IsNullOrEmpty(model.OpenID))
            {
                sb.Append(" AND T1.OpenID=@OpenID");
                dparams.Add("OpenID", model.OpenID, System.Data.DbType.String);
            }

            if (model.HCity > 0)
            {
                sb.Append(" AND T1.City=@City");
                dparams.Add("City", model.HCity, System.Data.DbType.Int32);
            }
            else if (model.HProince > 0)
            {
                sb.Append(" AND T1.Proince=@Proince");
                dparams.Add("Proince", model.HProince, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.BCreateDate))
            {
                sb.Append(" AND T1.CreateDate>@BCreateDate");
                dparams.Add("BCreateDate", EConvert.ToDateTime(model.BCreateDate).Date, System.Data.DbType.DateTime);
            }

            if (!string.IsNullOrEmpty(model.ECreateDate))
            {
                sb.Append(" AND T1.CreateDate<@ECreateDate");
                dparams.Add("ECreateDate", EConvert.ToDateTime(model.ECreateDate).Date.AddDays(1), System.Data.DbType.DateTime);
            }
        }
    }
}